/**
 * Created by LUOLIANG on 2016/6/14.
 */

var billlist = avalon.define({
        $id: "billlistController",
        user: "",
        income: 0,
        pay: 0,

        userInfo: function () {
            billlist.load('day');
            $.ajax({
                type: "post",
                url: "/CounterDrops/weuser/currentUser",
                data: {},
                success: function (data) {
                    if (data.success == true) {
                        billlist.user = data.model;
                    } else {
                        toastr.warning(data.msg);
                    }
                }
            });
        },

        outlogin: function () {
            $.ajax({
                type: "post",
                url: "/CounterDrops/weuser/outLogin",
                data: {},
                success: function (data) {
                    if (data.success == true) {
                        window.location.href = "/CounterDrops/index/wechat/login";
                    } else {
                        toastr.warning(data.msg);
                    }
                }
            });
        },

        load: function (type) {
            var counter = 0;
            // 每页展示4个
            var num = 4;
            var pageStart = 0, pageEnd = 0;

            // dropload
            $('.content').dropload({
                scrollArea: window,
                domDown: {
                    domClass: 'dropload-down',
                    domRefresh: '<div class="dropload-refresh">↑上拉加载更多</div>',
                    domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
                    domNoData: '<div class="dropload-noData">已加载所有</div>'
                },
                loadDownFn: function (me) {
                    $.ajax({
                        type: 'GET',
                        url: '/CounterDrops/weOrder/orderlist',
                        data: {
                            type: type
                        },
                        success: function (data) {
                            var result = '';
                            counter++;
                            pageEnd = num * counter;
                            pageStart = pageEnd - num;
                            if (data.items.length == 0) {
                                // 锁定
                                me.lock();
                                // 无数据
                                me.noData();
                            } else {
                                for (var i = pageStart; i < pageEnd; i++) {
                                    if (data.items[i].incometype == "收入") {
                                        billlist.income += data.items[i].charges;
                                        data.items[i].charges = "+" + data.items[i].charges;
                                    } else {
                                        billlist.pay += data.items[i].charges
                                        data.items[i].charges = "-" + data.items[i].charges;
                                    }
                                    result += '<tr>' + '<td>' + data.items[i].ordertype + '<td>'
                                        + '<td>' + data.items[i].charges + '<td>'
                                        + '<td style="font-size: 0.8em">' + data.items[i].createtime + '<td>'
                                        + '</tr>';
                                    if ((i + 1) >= data.items.length) {
                                        // 锁定
                                        me.lock();
                                        // 无数据
                                        me.noData();
                                        break;
                                    }
                                }

                            }
                            // 为了测试，延迟1秒加载
                            setTimeout(function () {
                                $('.lists').append(result);
                                // 每次数据加载完，必须重置
                                me.resetload();
                            }, 1000);
                        },
                    });
                }
            });
        },

        querytype: function (type) {
            $(".dropload-down").remove();
            $("table").html("");
            billlist.income = 0;
            billlist.pay = 0;
            billlist.load(type);
        }
        ,

        skip: function () {
            window.location.href = "/CounterDrops/index/wechat/billnotes";
        }
    })
    ;
billlist.userInfo();